package org.csu.gp_store.controller;

import org.csu.gp_store.bo.ItemBO;
import org.csu.gp_store.common.CommonResponse;
import org.csu.gp_store.entity.Item;
import org.csu.gp_store.service.ItemService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/items")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @GetMapping("items/{productID}")
    @ResponseBody
    public CommonResponse<List<Item>> getItemListByProductID(@PathVariable("productID") @NotNull String productID){
        return CommonResponse.createForSuccess(itemService.getItemListByProductID(Long.valueOf(productID)));
    }

    @GetMapping("/item/{itemID}")
    public CommonResponse<ItemBO> getItemByItemID(@PathVariable("itemID") @NotNull  String itemID){
        return CommonResponse.createForSuccess(itemService.getItemByItemID(Long.valueOf(itemID)));
    }

//    @PatchMapping("/status/{itemID}")
//    @ResponseBody
//    public CommonResponse<String>  updateItemStatus(@PathVariable("itemID") @NotNull String itemID,
//                                                    @RequestParam("status") int status){
//
//    }
//    @PatchMapping("/price/{itemID}")
//    @ResponseBody
//    public CommonResponse<String>  updateItemPrice(@PathVariable("itemID") @NotNull String itemID,
//                                                   ){
//
//    }
//    @PatchMapping("/image/{itemID}")
//    @ResponseBody
//    public CommonResponse<String>  updateItemImage(@PathVariable("itemID") @NotNull String itemID,
//                                                   @RequestParam("image") String image,){
//
//    }


    @PatchMapping("/item/{itemID}")
    @ResponseBody
    public CommonResponse<String>  updateItem(@PathVariable("itemID") @NotNull String itemID,
                                            @RequestParam("productID") String productID,
                                            @RequestParam("price") BigDecimal price,
                                            @RequestParam("status") int status,
                                            @RequestParam("image") String image,
                                            @RequestParam("style") String style){

        Item item=ItemBO.ItemBOToItem(itemService.getItemByItemID(Long.valueOf(itemID)));
        item.setStyle(style);
        item.setPrice(price);
        item.setStatus(status);
        item.setProductID(Long.valueOf(productID));
        item.setImage(image);
        String msg;
        if (itemService.updateItem(item)==1){
            msg="修改成功。";
            return CommonResponse.createForSuccessMsg(msg);
        }else {
            msg="修改失败，请稍后尝试。";
            return CommonResponse.createForError(msg);
        }
    }

    @DeleteMapping("/deletion/{itemID}")
    @ResponseBody
    public CommonResponse<String> deleteItemByItemID(@PathVariable @NotNull String itemID){
        if (itemService.deleteItem(Long.valueOf(itemID))==1){
            return CommonResponse.createForSuccessMsg("删除成功");
        }else {
            return CommonResponse.createForError("删除失败");
        }
    }

    @PostMapping("/addition")
    @ResponseBody
    public CommonResponse<String> insertItem(@RequestParam("productID") String productID,
                                             @RequestParam("price") String price,
                                             @RequestParam("status") String status,
                                             @RequestParam("image")String image,
                                             @RequestParam("style") String style){
        try {
            Item item =new Item();
            item.setProductID(Long.valueOf(productID));
            item.setPrice(BigDecimal.valueOf(Double.parseDouble(price)));
            item.setImage(image);
            item.setStyle(style);
            if (itemService.insertItem(item)==1){
                return CommonResponse.createForSuccessMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return CommonResponse.createForError("添加失败");
    }
}
